package com.qf.admin.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qf.common.domain.admin.vo.OptionsVO;
import com.qf.common.domain.admin.vo.RolePageVO;
import com.qf.admin.service.system.SysRoleService;
import com.qf.common.aspect.ApiOperationLog;
import com.qf.common.core.domain.ResponseResult;
import com.qf.common.core.page.BaseController;
import com.qf.common.core.page.PageResult;
import com.qf.common.domain.admin.dos.SysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: sin
 * @Date: 2024/10/13 16:01
 * @Description:
 **/
@Api(tags = "03.角色接口")
@Slf4j
@RestController
@RequestMapping("/admin/role")
public class RoleController extends BaseController {

    @Resource
    private SysRoleService roleService;

    @ApiOperation(value = "获取角色分页信息")
    @ApiOperationLog(description = "获取角色分页信息")
    @GetMapping("/page")
    public ResponseResult<PageResult> getRolePage(@RequestParam(value = "name", required = false) String name,
                                                  @RequestParam(value = "status", required = false) Integer status,
                                                  @RequestParam(value = "code", required = false) String code) {
        startPage();
        List<RolePageVO> roles = roleService.getRolePage(name, status, code);
        return ResponseResult.success(getData(roles));
    }

    @ApiOperation(value = "新增角色")
    @ApiOperationLog(description = "新增角色")
    @PostMapping("/save")
    public ResponseResult saveRole(@RequestBody SysRole role) {
        boolean result = roleService.save(role);
        return isOk(result);
    }

    @ApiOperation(value = "修改角色")
    @ApiOperationLog(description = "修改角色")
    @PutMapping("/update")
    public ResponseResult<Boolean> updateRole(@RequestBody SysRole role) {
        boolean result = roleService.updateById(role);
        return isOk(result);
    }

    @ApiOperation(value = "删除角色")
    @ApiOperationLog(description = "删除角色")
    @DeleteMapping("/delete")
    public ResponseResult deleteRole(@RequestParam(value = "id") Long id) {
        return isOk(roleService.removeById(id));
    }

    @ApiOperation(value = "获取角色下拉列表")
    @ApiOperationLog(description = "获取角色下拉列表")
    @GetMapping("/getAllRoleList")
    public ResponseResult<List<OptionsVO>> getAllRoleList() {
        List<OptionsVO> list = roleService.getAllRoleList();
        return ResponseResult.success(list);
    }

    /**
     * @Author: sin
     * @Date: 2024/10/13
     * @Description: 通过角色Id获取角色菜单权限
     **/
    @ApiOperation(value = "获取角色菜单权限")
    @ApiOperationLog(description = "获取角色菜单权限")
    @GetMapping("/getRoleMenu")
    public ResponseResult getRoleMenu(@RequestParam(value = "roleId") Long roleId) {
        return ResponseResult.success(roleService.getRoleMenuByRoleId(roleId));
    }

    /**
     * @Author: sin
     * @Date: 2024/10/13
     * @Description: 通过角色id修改角色权限
     **/
    @ApiOperation(value = "修改角色权限")
    @ApiOperationLog(description = "修改角色权限")
    @PutMapping("/updateRoleMenu")
    public ResponseResult<Boolean> updateRoleMenu(@RequestParam(value = "roleId") Long roleId,
                                       @RequestBody JSONObject object) {
        String s = JSON.toJSONString(object.get("menus")); //json转换有问题，需要重新转，并指明类型
        List<Long> menuIds = JSON.parseArray(s, Long.class);// 指定转换的类型
        return isOk(roleService.updateRoleMenu(roleId, menuIds));
    }
}
